package com.yujistu.fragment.appointfragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.yujistu.R;
import com.yujistu.activity.appointactivity.GroupDetailActivity;
import com.yujistu.adapter.BaseListAdapter;
import com.yujistu.adapter.GroupClassAdapter;
import com.yujistu.fragment.BaseFragment;
import com.yujistu.framework.helper.GroupClassHelper;
import com.yujistu.integration.bean.GroupClassEntity;
import com.yujistu.integration.request.ClassRequest;
import com.yujistu.network.StuRestClient;

import java.lang.reflect.Type;
import java.util.ArrayList;

import cz.msebera.android.httpclient.Header;

/**
 * Created by Win on 2015/11/6.
 */
public class GroupClassFragment extends BaseFragment<GroupClassEntity> {

    @Override
    protected String getURL() {
        return GroupClassHelper.Group_List;
    }

    @Override
    public BaseListAdapter getListAdapter() {
        return null;
    }

    private GroupClassAdapter mAdapter;
    ListView  mListView;

    View   mContentView;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

       mContentView = inflater.inflate(R.layout.fragment_group_class, container, false);

        return mContentView;
    }

    @Override
    public void onResume() {
        super.onResume();

        mListView = (ListView)mContentView.findViewById(R.id.groupClassLV);
        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                startActivity(new Intent(getActivity(), GroupDetailActivity.class));
                GroupClassListView();
            }
        });

        mAdapter = new GroupClassAdapter(mActivity);
        mListView.setAdapter(mAdapter);
    }

    private void GroupClassListView() {
        ClassRequest request = new ClassRequest();

        RequestParams params = new RequestParams();
        /*params.put("token", "token");
        params.put("page", "page");
        params.put("filter_order", "filter_order");
        params.put("filter_purpose", "filter_purpose");
        params.put("filter_price", "filter_price");
        params.put("filter_area", "filter_area");*/

       StuRestClient.get(getActivity(), request.getHttpUrl(), params, new AsyncHttpResponseHandler() {
           @Override
           public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {

           }

           @Override
           public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {

           }
       });
    }

    @Override
    public void initView(View view) {
         loadData(true);
         swipe_container.setRefreshing(true);
    }

    @Override
    protected boolean needDivider() {
        return false;
    }

    @Override
    public void onItemClick(AdapterView parent, View view, int position, long id) {
        super.onItemClick(parent, view, position, id);

    }

    @Override
    protected String getJsonArrayName() {
        return "courses";
    }

    @Override
    public ArrayList<GroupClassEntity> getArrayList(String jsonStr) {
        Gson  gson = new Gson();
        Type listType = new TypeToken<ArrayList<GroupClassEntity>>(){
        }.getType();
        ArrayList<GroupClassEntity> list = gson.fromJson(jsonStr,listType);
        return list;
    }
}
